home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Technotools
/
Technotools (Chestnut CD-ROM)(1993).ISO
/
lang_c
/
cnews013
/
cnews013.nws
< prev
next >
Wrap
Text File
|
1988-12-28
|
69KB
|
1,473 lines
C NEWS Vol. 1 Issue 13 December 27, 1988
*-------------------------------------------------------------*
| C NEWS - International C Electronic Newsletter/Journal |
| "Dedicated to the Art of C Programming" |
| |
| Founded 12/27/87 |
*-------------------------------------------------------------*
Table of Contents
The Heap: Messages from the Editor ..........................2
by Barry Lynch
Book Review: Software Engineering in C ......................5
by Barry Lynch
Magazine Review: Circuit Cellar Ink ..........................7
by Barry Lynch
Compression Utilities: One Sysop's View .....................8
by Barry Lynch
Commentary: ................................................10
by Arnie Cherdak
Program Design: A Utility: OpusGraf ........................12
by Jim Singleton and Barry Lynch
AWK: An Introduction Part I ................................16
By Dan Kozak
Message Threads from the C BBS: ............................22
by Users of the C BBS
- Changing a File Attribute - Chris Kryza
- Checking the Size of a Directory - Bob Smith
Interactive C Graphics: Part I .............................23
by Scott Houck
Article Submission Standards ................................28
Address's ...................................................29
Distribution Points .........................................30
User Response Form ..........................................31
C News is an Electronic Journal published by the C BBS in
Burke, VA on a monthly basis. The subject for C News is the C
programming language, as well as any derivatives like C++.
All readers are encouraged to submit articles, reviews, or
comments for submission. C News is freely distributed, but
can not be sold for a profit, or cannot have a charge assessed
to cover distribution costs. To do so is in direct violation
of the License agreement. Copies of which are available from
the C BBS. This publication is Copyrighted under U.S
Copyright Law.
Page 1
C NEWS Vol. 1 Issue 13 December 27, 1988
=====================================================================
THE HEAP: Messages from the Editor
=====================================================================
TABLE OF CONTENTS:
- Status of the C BBS
- What's in this issue of C News
- User feedback revisited in gentler terms
- Postcards wanted
- Software Distribution System (SDS) distribution of C News
- New feature starting with this issue of C News
- What is planned for Year 2 of C News
[STATUS OF THE C BBS: OR HOW A HARDISK CRASH MAKES ME FEEL HUMBLE ..]
It's been a long couple of weeks here at the C BBS, a
catastrophic hardware failure caused the complete loss of the C
BBS itself. Luckily, I had most of the C source files on disk
and was able to recreate the BBS. Some minor things still need
to be completed, but the board is operational and once again C
News is being cranked out on an infrequent basis.
One thing the crash did was delay this issue of C News by a
month or so. Instead of canvassing for articles, I was out
drumming up old C BBS C src files and attempting to get my echo
and netmail areas to work. No, I did not have a backup, who
does when it is 80 floppies!!
[WHAT'S IN THIS ISSUE OF CNEWS?]
This issue of C News features book and magazine reviews,
also in this issue three different multi-part articles are
presented: Software Design, Interactive C Graphics, and AWK.
We were blessed with three seperate projects at the same time,
and this allows us to split the articles apart and have material
for a couple of issues of C News.
This also allows you the reader the opportunity to provide
some feedback to the authors that can be included in a future
issue of C News! I hope that you will enjoy these articles as
much as we have in bringing them to you.
[USER FEEDBACK]
Remember back a few issues in this column, I mentioned how
little feedback we were receiving? Well in the last month,
postcards, letters and electronic mail messages have been
arriving from around the globe. For instance today, a user
logged in from Ireland and praised the editors and writers of C
News for putting C News together. This is the sort of feedback
that we have been looking for, nothing makes a volunteer feel
better than a nice compliment on his or her efforts. All
writers of C News are volunteers and a little feedback is
needed. Keep up the good work, and keep those postcards
Page 2
C NEWS Vol. 1 Issue 13 December 27, 1988
coming!
[POSTCARDS WANTED!]
Postcards are still wanted!! To date postcards have been
received from Arlington, Virginia, New York, New York, and
Montreal, Canada. So the breakdown to date is:
United States World
Virginia Montreal, Canada
New York
Maryland
[SOFTWARE DISTRIBUTION SYSTEM (SDS) DISTRIBUTION OF CNEWS]
C News will begin dsitribution in the Northeast section of
the country via SDS with this issue. This is in direct response
to a netmail message that I received from the coordinator of SDS
for the NE. This is a big step for C News, as it brings it
closer to the intended audience. C News was originally intended
for the readers of the Fidonet C Echomail conference, but has
grown to be an international electronic journal that tranverses
networks and systems. Please upload C News wherever you can,
and let's spread the word......
[NEW FEATURE STARTING WITH THIS ISSUE OF CNEWS]
Following in the foot steps of "Byte" and "Circuit Cellar
Ink", I have decided to place message threads of interest to C
programmers, from my BBS in this newsletter. This allows
users/readers that do not log onto the C BBS, the chance to see
a question and the responses that were received.
The first message thread was started by Chris Kryza,
concerning how to change a file attribute, the second thread
deals with some floating point problems during execution of a
program in TurboC, and the third is a check directory program.
Enjoy these tidbits and if you see somthing that needs
correcting or further explanation do not hesitate to ask.
[What is Planned for Year 2, of C News?]
Well, believe it or not we have almost come full circle.
Back last December I started C News out of the frustration of
reading the same questions over and over again in the C_Echo.
Little did I know, or dare to dream that C News as it is to day
would exist. This is not a commercial product nor will it ever
be. It is not intended to compete with the multitudes of
programming journals and magazines that exist, but compliment
them. Let's welcome the new year by giving a hand to the users
of the C BBS and readers of C News that have taken the time to
contribute!! <clap, clap, clap>
Page 3
C NEWS Vol. 1 Issue 13 December 27, 1988
C News Contributors (1988)
David Nugent - Melbourne, Australia
Marshall Presnell - Parts unknown at the moment
David Cheslow - Dallas, Texas
Arnie Cherdak - Silver Spring, Maryland
Bill Mayne - Silver Spring, Maryland
Jim Singleton - Arlington, Virginia
Jerry Zeisler - Herndon, Virginia
Jack Hess - Dallas, Texas
Dan Kozak - Arlington, Virginia
and
Scott Houck - Washington, D.C.
Page 4
C NEWS Vol. 1 Issue 13 December 27, 1988
======================================================================
BOOK REVIEW: By Barry Lynch
======================================================================
Title: Software Engineering in C
Author: Peter A. Darnell
Philip E. Margolis
ISBN Number: 0-387-96574-2
Publisher: Springer-Verlag
New York-Berlin-Heidelberg
Back in Issue 10 of C News, I stated in the Book review
column: "The last thing I needed - and my checkbook - was to
buy another computer book" Well, this is still the case but I
can definitely make an exception in with this book.
Rarely is it that I can pick up a book, and in a few
moments know that the book is worth the cover price. To often
in our profession we are forced to pay good money for books that
are not worthy of our time. This is not the case with "Software
Engineering in C". The title is a tad deceiving, as it seems to
indicate that the book is another dry text on software
engineering principles, of which a few are on the bookcase to my
right. < For sake of brevity I will refer to the book in the
remainder of this document as "seC">. seC is an attempt to
explain proper software engineering principles using one of the
more popular languages on the market today. For instance: In
Chapter 12 - Software Engineering, sections are included on:
- Product Specification
- Software Design
- Project Planning Cost Estimation
- Software Tools for Software Production
- Debugging Techniques
- Testing
- Performance Analysis
- Documentation
- Source Control and Organization.
All topics that are relevant to the software engineer
professional. The authors use a "Useful" application to flesh
out the ideas and concepts presented in the book: A C
Interpreter. To often in books of this nature, the example
application given is a "Mailing List Manager", or a "Graphics
Menu System". This is a meaty application for a meaty book.
Messer's Darnell and Margolis have done an excellent job in this
regard.
Is this book for the beginner? The title suggests not, not
like "QuickC Programming for the IBM" does. But the authors
have attempted to create the seeds of excellent programmers
early in the process, instead of just gearing the book to more
advanced users. They also cover in some detail the proposed new
ANSI standard for C. In one of the appendices included in the
book, syntax of ANSI C is outlined. While in a later appendice,
the differences between K&R C and ANSI C are outlined. This
sort of detail can only add more substance and reason for me to
break my budget and purchase a copy. I highly recommend that
Page 5
C NEWS Vol. 1 Issue 13 December 27, 1988
you do the same.
**** Editor's Note: This book will be used extensively in the
series on Product design that starts with this issue. BL
Page 6
C NEWS Vol. 1 Issue 13 December 27, 1988
=====================================================================
MAGAZINE REVIEW: By barry Lynch
=====================================================================
Magazine: Circuit Cellar Ink
Issue: Vol 1, No. 5
Publisher: Circuit Cellar Incorporated
4 Park Street
Suite 20
Vernon, CT 06066
(203)875-2751
At this time my favorite journal is "Micro Cornucopia". I
also favor "C Users Journal" and the "Harvard Business Review".
Each month without fail each of these publications is read,
re-read and cataloged away for future reference. Today, a new
journal threatened to join that select group: Circuit Cellar
Ink.
For those of us that remember Byte back when, Steve Ciarcia
was always one of the shining points of each issue. I can
remember back in the early 80's, drooling over a computer that
Mr. Ciarcia had put together, and hoping that some day one would
find it's way to my door. (This was right before the IBM Pc was
introduced I believe) Being very interested in Software and
Hardware I find his columns to be of great interest, as he
dreams up new wonderful electronic gizmos to build. Sort of
reminds me of those wonderful hacker days, before the word
became a mark of distrust, instead of pride.
This magazine is Mr. Ciarcia's attempt to provide a
technical journal that caters to the tinkers, and thinkers that
exist in today's computer society. I for one am not interested
in how to program a Lotus spreadsheet, but creating a Home
control system via a power line interface is more my speed. In
the issue that I read today, the lead article was on a project
called "ROVER" which stands for "remotely operated video-based
electronic reconnaissance". Imagine calling a video camera at
home, and watching it pan your yard at your control!! NEAT. If
these are the sort of articles that you like, then this little
known journal is for you. It is rather new and hard to find on
newsstands, but I found my copy in a B. Dalton Software Etc
store. If you cannot find it locally, consider calling the
publisher to see where it is located locally.
Page 7
C NEWS Vol. 1 Issue 13 December 27, 1988
=====================================================================
COMMENTARY: File Compression Utilities by Barry Lynch
=====================================================================
In the Bulletin Board community, there is one group of
utilities that are as indispensable as the modems that the BBS
Systems use: the File compression utilities. Two companies
have been the leaders in the use of these type of utilities:
SEA Associates and Phil Katz Ware. Both of these companies have
been involved in a legal dispute over the origin of Phil Katz's
code. Needless, to say, due to the fact that almost every DOS
based BBS system uses either ARC or PKARC, this legal action has
been followed with close scrutiny by most system operators and
users alike.
This commentary is not on what package to use, but what I
have chosen to do here at the C BBS and with C News. Starting
with this issue of C News, each issue will be compressed and
contained within a library using ZOO. ZOO is another of the
File Compression utilities that is currently available, to the
sysop and user. It is Public Domain, except for it's
restriction on pay per hour BBS systems, and the C source is
available. Here at the C BBS the latest version can be found
along with utilities to make the transition from ARC type files
to ZOO.
Why did I decide to go with ZOO? Basically because the
entire SEA vs Pkware made me ill. I have absolutely no problem
with making a profit off of a product which you create. But the
lawsuit that ensued and the bickering that went back and forth
between both parties, just made me change to a librarian utility
that was not under so much heat. Zoo is public domain as is C
News and it fit. Simple, and effective, the "ARC" wars have
just begun I am afraid, and what you will see now is a rash of
programs that are based upon the essentially the same
algorithms, that claim to be faster, better and cheaper. I for
one would like to see a NEW data compression algorithm that
makes these claims, that would be news.
So since I decided to use ZOO, would the file sizes the
smaller/bigger than the ARC's that I currently have? Is ZOO
faster,slower, or the same speed in compressing files? These
are the questions that I will answer in the next paragraph.
I chose to use three C source files in a separate
sub-directory as my test files. I compressed them all into one
file using the following utilities: SEA Assoc. ARC versions
5.2, and 5.32, Phil Katz's PKARC, PAK version 1.0, and ZOO
version 2.01. The source files listed with sizes are contained
in Table 1.0, and the compression results are listed in Table
1.1.
Page 8
C NEWS Vol. 1 Issue 13 December 27, 1988
Table 1.0 - Source files with sizes.
SOURCE FILES
Editor.c - 21983
Menu.c - 4150
Window.c - 9339
Table 1.1 - Results of Compression Utilities.
Program Compression Size of
Ratio of each Test.Arc
src file.
SEA ARC 5.2 Editor.c 67% 13052
Menu.c 47%
Window.c 60%
SEA ARC 5.32 Editor.c 69% 13052
Menu.c 47%
Window.c 54%
PAK 1.0 Editor.c 69% 12950
Menu.c 48%
Window.c 54%
PKARC Editor.c 68% 13007
Menu.c 48%
Window.c 59%
ZOO 2.01 Editor.c 68% 13423
Menu.c 47%
Window.c 54%
Page 9
C NEWS Vol. 1 Issue 13 December 27, 1988
=====================================================================
COMMENTARY: James L. Buie - TTL Inventor by Arnie Cherdak
=====================================================================
I ran across this obituary in an old issue (mid-1988) of
one of those electronics industry newspapers that I get at the
office. I was browsing idly one day after a tough meeting...
***************************************************************
PANORAMA CITY, Calif. - James Buie, who invented and
patented transistor-to-transistor (TTL) logic in the early
1960s died here last week at the age of 68.
Mr. Buie's invention, TTL, was to become the most widely
used form of circuit logic, and was broadly licensed throughout
the then-emerging integrated circuit industry by TRW, for which
the inventor worked.
James L. Buie was born in Hollywood, Calif. in 1920 and
educated at Hollywood High School and Los Angeles City College.
After World War II, where he rose to the rank of Lieutenant
Commander as a naval pilot, he enrolled at the University of
Southern California for a bachelor of science degree in
electrical engineering.
In 1954, Mr. Buie joined the Ramo-Wooldridge Corp., a TRW
predecessor company, where he was assigned to Pacific
Semiconductor Co. Along with inventing TTL, Mr. Buie was
involved in the development of dielectrically isolated
integrated circuits, a single-chip parallel multiplier, a
single-chip analog-to-digital converter and triple-diffused
bi-polar devices.
Until 1977, Mr. Buie served as technical leader of TRW's
Microelectronics Center, which he helped to establish. He was
also instrumental in the startup of the LSI Products Division,
where he worked until his retirement in 1983.
Mr. Buie is survived by his wife, Ione, a son, John, a
daughter, Janet Reasoner, a sister Joan Runsvold, and three
great-grandchildren.
***********************************************************
Page 10
C NEWS Vol. 1 Issue 13 December 27, 1988
=====================================================================
COMMENTARY: James L. Buie - TTL Inventor by Arnie Cherdak
=====================================================================
I was virtually dumbstruck when I finished reading this.
How could this be, that the man who invented TTL, one of the
inventions that made modern digital electronics and computer
technology a practical enterprise, died and only got 9
column-inches in the back of a trade journal (name forgotten--my
apologies) for his remembrance.
I never knew James Buie. I didn't even know he did what he
did until I read his obituary. But now I sure know who he was
and I, for one, won't forget his contribution. I hope his
great-grandchildren will know about and remember him as well
because they can have pride in their great-grandfather's
accomplishments as an engineer who put an industry to work.
**********************************************************
EDITOR'S NOTE: I agreed to publish this article on James
Buie because I felt it was important to
remember the man that helped to create the
hobby or vocation that we all enjoy. BL.
***********************************************************
Page 11
C NEWS Vol. 1 Issue 13 December 27, 1988
=====================================================================
PROGRAM DESIGN: OpusGraf a Project: Part I:
=====================================================================
by Barry Lynch and Jim Singleton
Editor's Note: This article is the first in a continuing series on
software development. OpusGraf is a opus log utility
that creates a bargraph showing utilization for a given
day. This graph can then be output to a file to be used
as a bulletin. This project will be of interest to all
users interested in graphics.
[Introduction]
The idea for this article came from discussion at one of
the C BBS user's meetings that are held every month here in
Virginia. The idea of an article that discussed the evolution
of a software project, was one that quite a few people showed an
interest in. Specifically, Jerry Zeisler in Issue 12 of C News
made some interesting observations in his first attempt at a C
application.
Those observations were the motivation that I needed to
sit down and create this project. The other motivation was that
when I ran a PC Board Bulletin Board System, a utility was
available to show the utilization of the board. And the
bulletin that it generated was one of the more popular bulletins
on the system.
Therefore, this article is the first in a series that will
show the development of a utility and outline some of the right
things to do, and show some of the things that went wrong. The
general plan for content of each article is as follows:
Issue Article/Subject
13 Introduction, and Product Specification
14 Detailed Design Document and Flow of Program
15 First BETA release of OpusGraf
16 Bug reports and Corrections
17 Final Release and a Post Mortem Analysis
This syllabus allows me to show a complete detailed view of
the project, and provide some educational value. I believe that
it will also prove to be interesting reader for Fidonet
developers and C news readers.
SECTION I: The IDEA!
How often do you sit at a desk and come up with an idea
for a program or great application that we all need? What do
you do? Write down your thoughts then and there, or store them
away for a later date? I personally carry a Black Composition
book - the kind that you find in school bookstores - and make
entries as needed. At the end of week, I go through the entries
Page 12
C NEWS Vol. 1 Issue 13 December 27, 1988
and make a list of the ones that I want to work on, and a
scratch out those that either I have no time for, or was really
banana wackers to think of.
Once an idea has made it to the "To Do" list, then it is
time to think of exactly what it is I am trying to accomplish.
This is how OpusGraf started, a brief thought that the OPus
sysops needed such a utility, and it would make a good C News
series. So, what is it that I want to accomplish? Well, let's
see:
- A Utility that will read the previous day's opus.log
and generate a Bar-Graph showing usage.
- The ability to redirect the Bar-Graph to a disk file
for showing as a Bulletin.
- The option for the sysop to break out the type of utilization
to be shown: i.e.: Human Caller/Mail/FR/Maintenance, or All.
[Product Specification]
Now that the idea is written down and a short but simple
list of objectives is written the design work can begin. The
first thing to do is to create a rough outline of what you want
the program to do. Listed below is a simple outline that can be
used as a guideline:
- Abstract of project
- Command line interface
- Input file syntax
- Screen design
- Output file format descriptions
- Interactive command language (If needed)
- Error messages
- Future extensions
Now for a sample product specification for OpusGraf, based
upon the outline listed above.
1. Abstract
The intent of this project is to create an Fidonet Compatiable
graphic utilization utility. It will support:
o Opus BBS systems
o Fido BBS systems
The goal of this project is to show some principles of software
engineering and project management while creating a useful Fidonet
based Utility.
2. Command Line Interface
The command line does not need to include any parameters, unless the
user would like to overide some values that exist in the configuration
file. Valid command line parameters are:
Page 13
C NEWS Vol. 1 Issue 13 December 27, 1988
-A Tells Opusgraf to use all categories of util-
ization, i.e.: Human Caller/Machine etc.
-F<Path:filename> Path and filename of the configuration file
if not the default Opus/Fido/Opusgraf
directory.
-S Direct output to screen only, no output file
is created.
3. Input file syntax
File #1: Opus.log
The input file will be the Opus.log file created by the OPus BBS
system. <I will look for a technical description fof the format
in the FTSC documents that outline the technical details of Fido
net.>
File #2: opusgraf.cfg
This is the Opusgraf configuration file that contains certain para-
meters needed by the Opusgraf program. The layout is the same that
is used by other OPus/Fido utilities, for ease of familiarity and
use by the end user.
;***************************************************************
;*
;* OpusGraf Ver 1.0 Configuration file
;*
;* Written by B. Lynch and J. Singleton of the C BBS 109/713
;*
;* Purpose: This file is used by Opusgraf.exe to determine what
;* the default operating parameters are.
;*
;* Created for an article in C News Issue's 13-17 (12/88-5/89)
;*
;*
LOG c:\opus\opus.log /* The actual opus or fido log to be used */
ALL /* accumulate all utilization into hourly calculations */
;NOTALL /* break out the different utilizations by hr by % */
BOARD The C BBS /* Your board name */
SYSOP Barry Lynch /* Your name */
;LOGO "The Home of C BBS" /* Logo or motto of your board */
;
4. Interactive Command Language
Not needed in this application.
5. Error Messages
The Opusgraf program supports the following error codes:
1 - No OpusGraf.cfg file found
2 - Log file not found - as specified by config file
3 - Not enough memory to run utility
Page 14
C NEWS Vol. 1 Issue 13 December 27, 1988
4 - Unable to write out disk file - Not enough Free disk space
5 - Incorrect command line values <show help screen>
Once you have completed the rudimentary design, a detailed design
document is needed to outline the algorthims used, libraries, compiler
etc. We will save that document for the next issue of C News.
Page 15
C NEWS Vol. 1 Issue 13 December 27, 1988
=====================================================================
An Introduction to AWK by Dan Kozak
=====================================================================
Why AWK?
Every programmer spends more time than they will admit
doing rather mundane tasks. First you hear a rhythmic clicking
from their keyboard, going faster than any programmer could
possibly type unless it was a totally repetitive job. "What are
you doing?" "Ummm, well I found a much better way to lay out
these data files, but I have to go in and indent like so, put a
tab here and two newlines as a delimiter and then . . ." Sound
familiar?
You find yourself doing all kinds of data formatting
manually that you should really get the computer to do for you
because it's just too much bother to write a program to do it
and anyway, you only have to do it once (sure). AWK is great
for quick data transformations, validations, reports and also
for prototyping larger programs. You may find that knowing AWK
will make jobs like this so simple that the equation tips in
favor of ACTUALLY WRITING A PROGRAM TO DO THE JOB FOR YOU!
(Remember: computers are supposed to make life easier.)
[What is AWK?]
AWK is an interpretive programming language that was
developed by Alfred Aho, Peter Weinberger and Brian Kernighan
(put the first letters of their last names together . . . ) at
Bell Labs.
Although originally designed for short, one-time use
programs, it has been extended to handle more complex jobs,
making it more of a complete language. Because of its
interpretive nature and the way some of its constructs work, it
is not especially fast, but when development time is more
important than run-time (i.e. you don't use the program all the
time) and the amount of data isn't overwhelming AWK's
performance is fine. Although it's origin as a UNIX tool is
shown in it's use of regular expressions and a C-like syntax, it
is applicable to other operating systems as well; in particular
there are implementations under MS-DOS (without which I wouldn't
be writing this).
[How does AWK work?]
In AWK, much of the work of writing a program is done for
you by the structure of the language. Each program consists of
a series of patterns and actions, the input file(s) are read in
and every time a pattern is matched the corresponding actions is
taken. An AWK program is an implicit loop that, in pseudo code
looks like this:
(optionally do something here before we start)
while (not end of input file(s) )
Page 16
C NEWS Vol. 1 Issue 13 December 27, 1988
if (there is a pattern match)
take the coresponding action
(optionally do something here when we're done)
[What are patterns?]
A pattern is a way of specifying "this line of input belongs
in set A and that line does not". When you apply a pattern to a
line of input the result is a logical value (TRUE/FALSE) that
determines whether the coresponding action should be taken.
One kind of pattern is the regular expression, a notation
for describing a pattern to match that is used throughout UNIX
(MS-DOS users: "wildcarding" of filenames is a very small
subset of UNIX regular expressions). A regular expression is
usually delimited by slashes ("/") and specifys, using both
normal characters and "meta-characters" a pattern to look for.
So a simple AWK program might look like this:
/UNIX/ { print }
^ ^
| +--action
pattern
This would print every line in the input file that had the
pattern "UNIX" in it. Actually the default action (we'll get to
those soon) is print so this example could be written at the
command line level like this:
awk "/UNIX/" input.fil
(for the rest of this article, though, I will continue put the
superfluous { print } because it reads clearer.) The default,
outer level pattern is "match all lines" so this:
{ print }
would simply print all lines. Patterns can be compound like
this:
/UNIX|AWK/ { print } # the | symbol is a logical OR
The complete syntax of regular expressions is more than we
can go into here, but there is ample UNIX literature on the
subject.
Patterns don't have to be regular expressions, they can be
any kind of expression. AWK automatically performs field
splitting on the input lines (the default delimiter is "white
space" [space, tab, newline] but can be changed). NF is a
built-in AWK variable that is the number of fields in the input
line. This AWK program:
NF < 10 { print }
will print all input lines that have less that 10 fields. Field
splitting also gives you access to the indivdual fields in an
input line via the $0, $1, $2, . . ., $n variables. $0
Page 17
C NEWS Vol. 1 Issue 13 December 27, 1988
represents the entire input line, while $1 represents the first
field, $2 the second, and so on. As an example:
$1 == "AWK" { print }
would print all lines whose first field is the word AWK. Of
course more complex patterns can be fashioned using the AWK's
logical operators ||, &&, and so on (these perform the same
functions they do in C).
[What are actions?]
Actions are the part of AWK that most closely resemble other
programming languages, in particular C. Actions consist of
function calls, assignments and conditionals. We've already
seen the print function at work, but let's look at another
output function, one that should be familiar to C programmers.
{ printf("%3d: %s\n",NR, $0) }
(the built-in variable NR is the number of records read) This
example would print the input file with every line preceded by
its line number. Assignments also work like C, except that
variables need not be declared and have an initial numeric value
of 0 and an initial string value of "" (the empty string).
Here's a quick word count program:
{
words += NF
chars += length($0) + 2 # DOS uses a CR-LF combination
}
END { print "Characters:",chars,"Words:",words,"Lines:",NR }
Here we've introduced variables (except for the fact that
all variables in AWK have both a string and a numeric
representation that are used according to context, there's not
much say about these, you just use them), assignments, the
length function (one of several built-in functions), the comment
character (#) and the END pattern. The BEGIN and END patterns
are matched when the program starts, before any input has been
read and after all input has been read, respectively.
By making assignments to the field variables ($1, $2 . .
.), you can replace or create those fields:
{ $(NF+1) = "!EOL!"
print
}
This program adds the string "!EOL!" the end of every line and
then prints it.
Just to give you a feel for the kind of things actions can
do, AWK provides 14 built-in variables (we've seen NF and NR
already) that give access to the command line arguments, the
filename of the current input file, the field and record
separators for input and output and the default format for
numbers, 9 built-in math functions including random numbers, 12
built-in string functions (we've used length()) that provide a
Page 18
C NEWS Vol. 1 Issue 13 December 27, 1988
variety of search and replace functions and all the control flow
statements that exist in C (if, while, for do . . . while,
break, continue) except for switch. While space prohibits us
from looking at all of these, you should be able to see that AWK
provides a wide range of tools.
[Associative Arrays]
The way AWK handles arrays is different enough to merit its
own discussion. Arrays in AWK are one dimensional (although
there is a feature to simulate two dimensional arrays, we won't
go into that here), can hold numbers or strings and need not be
declared. However, AWK arrays are subscripted by strings not by
integers (this gives them some of the capabilities of SNOBOL4's
"associative memory" and that's why they are called associative
arrays). The best way to understand this is an example from
"The AWK Programming Language":
/Asia/ { pop["Asia"] += $3 }
/Europe/ { pop["Europe"] += $3 }
END { print "Asian population is", pop["Asia"], "million."
print "European populations is",pop["Europe"],
"million"
}
When provided with test data containing a country, it's area in
thousands of square miles, it's population in millions and the
continent it is in formatted like this:
USSR 8649 275 Asia
Canada 3852 25 North America
China 3705 1032 Asia
USA 3615 237 North America
Brazil 3286 134 South America
India 1267 746 Asia
Mexico 762 78 North America
France 211 55 Europe
Japan 144 120 Asia
Germany 96 61 Europe
England 94 56 Europe
this program produces the output:
Asian population is 2173 million.
European populations is 172 million
There are also some special constructs for use with arrays
like the "in" operator, used to determine whether a particular
subscript exists in an array, "for (var in array)" which will
loop through all the subscripts in an array, "delete" to remove
an element from an array and "split()", to divide a string up
and place it's fields in an array. Array operations are
powerful in AWK, but sometimes hard to understand, so refer to
"The AWK Programming Language" for the details (something you'll
probably have to do more than once :-).
Page 19
C NEWS Vol. 1 Issue 13 December 27, 1988
[Just the tip of the iceberg!]
There's more to AWK than I could go into here: user defined
functions, more complex output statements, reading from files
other than those specifed as input (allows you to interleave two
files to produce a report, for example), calls to the operating
system through the system() function (works like C), among
others. But, you can . . .
[Do it yourself]
Getting started with AWK is easy. There is a freeware
version by Rob Duff that is available with source from his BBS
(604-251-1816) which was used in the preparation of this
article. Try the programs in the article (the article itself
makes a good test input file) to see how you like it.
If you want to go further, the required reading is "The AWK
Programming Language" by Aho, Kernighan and Weinberger,
Addison-Wesley, ISBN #0-201-7981-X which serves as a manual and
tutorial for the language. There are also several commercial
DOS implementations of AWK, most notably Mortice Kerns Systems'
and Polytron's PolyAWK.
[An example]
In closing I leave you with an example I whipped up in
response to a question on the FidoNet C echo. This program
counts the lines of code in C program(s) excluding comments. It
reports the total number of lines in each file and all together
with and without preprocessor directives. The FILENAME built in
variable is the name of the current input file and the ~ and !~
operators are the "match" and "no match" operators for
specifying regular expressions within a program. The regular
expressions here are rather dense because C's comment delimiter
"/* . . . */" shares a character with AWK regular expression
delimiter "/", so every occurence of "/" within a regular
expression must be quoted with the "\" character. But even with
that complication, this program was up and running in about 20
minutes.
Page 20
C NEWS Vol. 1 Issue 13 December 27, 1988
# count lines in a C program, not counting comments or form feeds
# does separate count of preprocessor directives
# if a preprocessor directive is commented out, it does not count
{
if (file == "") file = FILENAME
if (file != FILENAME) {
printf("Number of lines in %s is: %d\n",file,nl+ppd)
printf("Number of preprocessor directives is: %d\n",ppd)
printf("Number of lines excluding preprocessor directives is: \
%d\n\n",nl)
file = FILENAME
tnl += nl
tppd += ppd
nl = 0
ppd = 0
}
# don't count blank lines
if ($0 == "") { ; }
# don't count lines that are all comment
else if ($1 ~ /^\/\*/ && $NF ~ /\*\/$/) { ; }
# flag beginning of multi-line comment
else if ($0 ~ /\/\*/ && $0 !~ /\*\//) { in_comment = 1 }
# flag end of multi-line comment
else if ($0 !~ /\/\*/ && $0 ~ /\*\//) { in_comment = 0 }
# don't count while in comment
else if (in_comment) { ; }
# count preprocessor directives separately
else if ($1 ~ /^#/) { ppd++ }
# count everything else
else nl++
}
END { printf("Number of lines in %s is: %d\n",file,nl+ppd)
printf("Number of preprocessor directives is: %d\n",ppd)
printf("Number of lines excluding preprocessor directives is: %d\n\n",nl)
file = FILENAME
tnl += nl
tppd += ppd
printf("Total number of lines is: %d\n",tnl+tppd)
printf("Number of preprocessor directives is: %d\n",tppd)
printf("Number of lines excluding preprocessor directives is: %d\n",tnl)
}
Page 21
C NEWS Vol. 1 Issue 13 December 27, 1988
=====================================================================
CHANGING A FILE ATTRIBUTE IN C: by Chris Kryza
=====================================================================
ABSTRACT: Back in early November, Chris wanted to know how to
change a files attribute in DOS. Two solutions were presented:
1) use the ATTRIB command, and the 2) Use the _chmod function in
TurboC. This article outlines a program that Chris wrote to
accomplish the latter.
INTRODUCTION
From: Chris Kryza Rec'd
To: Bill Mayne Msg #44, 04-Nov-88 08:16EST
Subject: Archive Bit
Bill, thanks for you info re the attrib command. It is one of
those DOS commands that I long ago forgot about. After doing a
little research, I found a way to accomplish the same thing from
within a Turbo C program using the _chmod function defined in
io.h. This func takes three arguments: filename (char *), get
or set flag (int 0 or 1), and mask (to set archive bit: 0x20,
to clear same 0x00). You can also set or clear all other file
attributes via this function, i.e. hidden file, system file,
read-only, etc. Thanks for putting me on the right track!
CDK.
*** There is a reply. See #51.
From: Bill Mayne Rec'd
To: Chris Kryza Msg #51, 06-Nov-88 18:12EST
Subject: Re: Archive Bit
Glad you solved your problem with the archive bit. Obviously
both the attrib command and the function you mentioned are just
different ways to call the same DOS interrupt. It is amazing
that MS doesn't provide commands to use some other useful
interrupts, like renaming a file into a different directory.
Fortunately almost all C compilers provide a fairly simple
function to call any interrupt you want, though it is nice as in
this case to have a more specific function for what you want so
you don't have to worry about the registers yourself.
*** This is a reply to #44.
Page 22
C NEWS Vol. 1 Issue 13 December 27, 1988
=====================================================================
Interactive C Graphics using TurboC by Scott Houck. Part I
=====================================================================
I recently completed a college course in interactive
computer graphics. Most of the students were doing their
projects on Macintosh computers. Since I have an AT clone with
an EGA at home, I got permission to use my home computer for my
projects.
I had just gotten Turbo C 2.0, so this was a good
opportunity to acquaint myself with Turbo's graphics package.
In this installment of C News, I will present some basic
graphics concepts and show how to use Turbo C to implement
simple geometric transformations. The programs require a mouse
and either an EGA or VGA graphics adapter.
I am writing this article not as a graphics or C expert,
but as an intermediate C programmer who is learning about
graphics and in particular, Turbo C's graphics package. There
are no doubt many insights that others could provide, and I
welcome any comments or advice.
[MOUSE PROGRAMMING]
-----------------
I have a Microsoft mouse, and I used the Microsoft Mouse
programming guide for information on programming the mouse. I
have not looked into other types of mice, but I do know that my
program does not work correctly with the Logitech mouse. I
assume the Logitech mouse must be programmed differently.
The Microsoft mouse uses interrupt 33h. Each function is
numbered and specified by placing the function number in the AX
register. Other registers are used to specify function
parameters, if any. I used the int86() function to interface to
the CPU registers. I experimented with Turbo's pseudo variables
before using int86(), but got some surprising results in the
MouseStatus() function since registers containing return values
were being overwritten before the function returned. Be very
careful with pseudo variables! I could not figure out what was
happening until I generated an assembly language listing.
All mouse functions in the program begin with "Mouse" as a
prefix. Here is a summary of the mouse functions I created:
MouseReset -- Resets the mouse to its defaults, tells you whether
the mouse hardware and software are installed or not, and also
returns the number of buttons in the mouse
MouseOn -- Shows the mouse cursor
MouseOff -- Hides the mouse cursor
MouseStatus -- Returns the state of the left and right buttons
and the horizontal and vertical coordinates of the cursor
Page 23
C NEWS Vol. 1 Issue 13 December 27, 1988
MouseWaitForPress -- A high level routine that loops until
the user presses a button
MouseWaitForRelease -- A high level routine that loops until the
user releases a button
MouseSetHorizPos -- Sets the minimum and maximum horizontal
cursor coordinates on the screen
MouseSetVertPos -- Sets the minimum and maximum vertical cursor
coordinates on the screen
MouseRestrict -- A high level routine that restricts the cursor
to the viewport
MouseFree -- A high level routine that frees the cursor to move
about the entire screen
[DEFINITION OF THE PROJECT]
-------------------------
The program I am presenting allows you to manipulate two
types of objects (a triangle and a square) on the screen. You
can create an object, delete an object, move an object, delete
all objects, print the objects' data structure in a file called
PROJ1.DAT, cancel an operation, and quit the program. The
objects are placed in a room. The "room" is square, 15 feet on
each side. The square is one foot on each side, and the
triangle is also one foot on each side.
[SOME BASIC GRAPHICS THEORY]
--------------------------
This program demonstrates basic geometric transformations
-- translation, scaling, and rotation. To understand the
program, some knowledge of graphics theory is needed. I will
not give exhaustive explanations here, but a general overview is
in order. Matrices are used in the program, so it is assumed
that the reader has some familiarity with manipulating
matrices.
The "room" is a "window" from the "world" that we are
trying to display on the screen. The room is square, 15 feet on
each side. Those are its world coordinates. The term "window"
has taken on several different meanings over the years. In
standard graphics terminology, however, a "window" is a usually
rectangular area that is a subset of your "world". In this
case, the window is the 15 foot square area in our world
coordinate system. We must transform the window to an area on
the graphics screen -- either the entire screen or a rectangular
area of the screen. This screen area is referred to as a
viewport.
When we manipulate objects on screen, we must take the
mouse coordinates (physical device coordinates -- 640x350 on
EGA, 640x480 on VGA) and transform them into world coordinates.
All changes to data about the objects are stored in world
Page 24
C NEWS Vol. 1 Issue 13 December 27, 1988
coordinates, which are floating point numbers. Screen
coordinates are always integers.
[TRANSLATION]
-----------
One of the basic graphics operations is translation, that
is, moving an object. To translate a point with coordinates (x,
y) by an amount Dx in the x direction and Dy in the y direction,
add the translation amount to each coordinate: (x+Dx, y+Dy).
In matrix notation, if p = [x y], p' = [x' y'], and T = [Dx Dy],
then p' = p + T. When translating an object, each point of the
object must be translated. For the triangle and square, each
vertex will be translated and the object will be redrawn from
the translated vertices.
[SCALING]
-------
Scaling means to change the size of an object. Scaling is
done in the x direction and the y direction. The scale factor
does not need to be the same for both directions. Scaling is
always done about the origin of the coordinate system. For
example, if you are reducing an object, not only will the lines
become smaller, but they will get closer to the origin.
Which brings us to snag #1. All this neat theory I learned
in graphics class was for a coordinate system in which the
origin (0, 0) is located in the bottom left corner of the
screen. However, PC graphics always seem to place the origin in
the upper left corner of the screen, so I often had to adjust
formulas to work with the PC.
To scale a point, multiply the x coordinate by the scale
factor for the x direction, and the y coordinate by the scale
factor for the y direction: x' = x * Sx, y' = y * Sy. In
matrix notation, if p = [x y] and
| Sx 0 |
S = | 0 Sy |, then p' = p * S.
[ROTATION]
--------
To rotate a point about the origin by an angle theta, the
formulas are: x' = x * cos(theta) - y * sin(theta), and y' = x
* sin(theta) + y * cos(theta). In matrix notation, if p = [x y]
| cos(theta) sin(theta) |
and R = | -sin(theta) cos(theta) |, then p' = p * R.
Rotation is always about the origin. To get an object to
rotate about its center, you must translate the center to the
origin, rotate, then translate it back to its original
position. If you do not translate the object to the origin
first, the object will rotate as if travelling along a circle
with its center at the origin.
Page 25
C NEWS Vol. 1 Issue 13 December 27, 1988
[HOMOGENEOUS COORDINATES]
-----------------------
Matrices are nice to work with in calculations, but they
are even more convenient if the coordinates are homogeneous
(expressed similarly). Then matrices may be combined to express
composition. For example, a translation followed by a scaling
followed by another translation can be done by multiplying the
respective matrices together and applying the product to the
point in question.
Two-dimensional homogeneous coordinates are expressed as 3D
matrices, where the last dimension is usually 1 (thought of as a
plane parallel to the x-y axis, the w=1 plane). So p(x, y, w),
w<>0, is the point in 3D. The 2D projection is (x/w, y/w). If
w=1, there is no need to divide, so the 2D point is just the
first two coordinates. Again, I will not go into any more
theory on this, but there is more explanation in the comments of
the code. See the functions Translate(), Rotate(), and
CalcW2Vmatrix() for more explanation. By the way, calculating
the window to viewport matrix brings us to snag #2: when you
use Turbo C's setviewport() function, the upper lefthand corner
is changed from its normal screen coordinates to a logical
coordinate of (0, 0). While I am sure this simplifies many
types of operations, it changes these matrix calculations a
bit.
[OPERATION OF THE PROGRAM]
------------------------
The user must first place at least one object in the room.
This is done by choosing the CREATE button with the left mouse
button, then clicking on the triangle or square, and finally
choosing a location in the room and clicking with the mouse
button to place the object. Deleting an object is done by
choosing DELETE, then clicking on the object in the room to be
deleted. MOVE is done by clicking on MOVE, then "dragging" the
object with the left button depressed, then releasing the
button. Rotating is performed by choosing ROTATE, then clicking
on an object to be rotated, then clicking or holding down either
the left or right button (rotates counterclockwise or
clockwise), then choosing QUIT when done. DELETE ALL asks for
confirmation through a dialog box. PRINT DATA creates a file
called PROJ1.DAT that contains all the vertices of all objects
in the room. CANCEL can be chosen to cancel any operation.
Choose QUIT to end the program.
Human factors were taken into consideration when writing
this program. At all times, a message box is shown at the
bottom of the screen to guide the user during program operation
and to display error messages. The dialog box asks for
confirmation. The dialog box is a good example of Turbo's
getimage() and putimage() functions.
All object information is stored in a linked list. When a
user creates a new object, it is placed at the end of the list.
When an object is deleted, it is also deleted from the list.
Finally, take note of how pick correlation is performed.
Page 26
C NEWS Vol. 1 Issue 13 December 27, 1988
Pick correlation refers to associating a mouse button click to
an object or an area on the screen. Each area has an "extent"
or rectangular boundary associated with it. When the user
clicks on the CREATE button, the program must determine which
button has been chosen and take appropriate action. Note that
choosing an overlapping object in the room will return only the
object on the top. More extensive programming would be required
to resolve ambiguous clicks.
[IN CLOSING]
I believe this program will give you a good feel for some
basic graphics techniques and Turbo C's implementation. Next
month I will present a simple paint program that will
demonstrate pull- down menus, "rubber banding" techniques, fill
patterns, and mouse cursor programming.
Page 27
C NEWS Vol. 1 Issue 13 December 27, 1988
=====================================================================
ARTICLE SUBMISSION STANDARDS AND ADDRESSES
=====================================================================
As I have repeatedly stated in this newsletter and previous
issues, I would like to see user-submitted articles, reviews or
questions. Listed below are the standards that should be
followed to make my job easier as an editor.
- Articles should be submitted in a ASCII non-formatted
file. (Margins 0-65 PLEASE)
- If the article include code fragments as examples. Then
you can include the entire source file if you like for
inclusion with the newsletter.
- Book or magazine reviews should follow the same format,
that is outlined in this issue. The publisher, author,
title, and ISBN number are a must.
- Compiler/and or product reviews, should include the
version number and manufacture. If possible, reviews
should include a sample program with benchmarks.
If you have any questions you can contact me at the
address's included on the next page.
Page 28
C NEWS Vol. 1 Issue 13 December 27, 1988
=====================================================================
HOW YOU CAN REACH THE AUTHORS OF C NEWS
=====================================================================
ADDRESSES
The C BBS is located at:
C BBS
% BCL Limited
P.O. Box 9162
McLean VA, 22102
or you can send netmail to:
1:109/713
or MCI Mail to: BCL Limited
Page 29
C NEWS Vol. 1 Issue 13 December 27, 1988
=====================================================================
DISTRIBUTION POINTS
=====================================================================
Board Name Number Net/Node Sysop
*** UNITED STATES ***
C BBS (703) 644-6478 1:109/713 Barry Lynch
McLean, VA
Eastern C Board (201) 247-6748 1:107/335 Todd Lehr
Exec-PC (414) 964-5160 .. Bob Mahoney
Milwaukee, WI
TAMIAMI (813) 793-2392 Gerhard Barth
Naples, FL
Sound of Music (516) 536-8723(2400) Paul Waldinger
(516) 536-6819(9600 Hayes V)
*** CANADA ***
Another BBS System (416) 465-7752 1:148/208 Mark Bowman
Toronto, Canada
*** EUROPE ***
Fido_N1_1 31-8350-37156 2:500/1 Henk Wevers
The Netherlands
DUBBS BBS 353-1-885634 n/a Stephen Kearon
Dublin, Ireland
*** AUSTRALIA ***
Sentry BBS 02-428-4687 ... Trev Roydhouse
(300-2400) Non-Mail Times
(300-19,200) Mail Hour (Trailblazer)
Page 30
C NEWS Vol. 1 Issue 13 December 27, 1988
=====================================================================
USER RESPONSE FORM
=====================================================================
This form will be included as a regular feature in all future
issues of C NEWS.
What did you think of the content of this Issue? _____________
_______________________________________________________________
What improvements can you think of that would make C News a
better tool for the C Community?
_______________________________________________________________
_______________________________________________________________
What is your favorite section or sections? ___________________
_______________________________________________________________
What don't you like about C News? ____________________________
_______________________________________________________________
Additional Comments: _________________________________________
_______________________________________________________________
_______________________________________________________________
_______________________________________________________________
Page 31